<template>
  <div class="login-box">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="80px"
      class="demo-ruleForm"
    >
      <h2>后台管理系统</h2>
      <el-form-item label="账号:" prop="username">
        <el-input v-model="ruleForm.username" autocomplete="off" />
      </el-form-item>

      <el-form-item label="密码:" prop="password">
        <el-input
          v-model="ruleForm.password"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="loginBtn"
          type="primary"
          @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
        <el-button class="loginBtn" @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from "vue";
//定义ruleForm的类型(src/type/login.ts)
import { LoginData } from "../type/login";
import type { FormInstance } from "element-plus";
import { login } from "../request/api";
import { useRouter } from "vue-router";
export default defineComponent({
  setup() {
    const data = reactive(new LoginData());
    // {
    //   //定义两个数据与v-model绑定
    //   ruleForm: {
    //     username: "",
    //     password: "",
    //   },

    // }
    //校验规则
    const rules = {
      username: [
        {
          required: true,
          message: "请输入账号", //离开输入框提示
          trigger: "blur",
        },
        {
          min: 3,
          max: 10,
          message: "账号的长度在3-10之间", //没满足条件提示
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          message: "请输入密码", //离开输入框提示
          trigger: "blur",
        },
        {
          min: 3,
          max: 10,
          message: "密码的长度在3-10之间", //没满足条件提示
          trigger: "blur",
        },
      ],
    };
    //登录
    const ruleFormRef = ref<FormInstance>();
    const router = useRouter(); //-->$router
    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return; //如果没有,直接返回，不发送请求
      //对表单的内容进行验证
      //valid布尔类型,为true表示验证成功,反之
      formEl.validate((valid) => {
        if (valid) {
          // console.log("submit!");
          //调用请求数据(执行api.ts里面的service)
          login(data.ruleForm).then((res) => {
            console.log('-------',res);
            //将token进行保存
            localStorage.setItem("token", res.data.token);
            //跳转页面,首页
            router.push("/");
          });
        } else {
          console.log("error submit!");
          return false;
        }
      });
    };
    //重置
    const resetForm = () => {
      data.ruleForm.username = "";
      data.ruleForm.password = "";
    };
    //动态绑定数据
    return { ...toRefs(data), rules, ruleFormRef, resetForm, submitForm };
  },
});
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  background: url("../assets/bg.png");
  padding: 1px;
  text-align: center;
  //登录表单样式
  .demo-ruleForm {
    width: 500px;
    margin: 200px auto;
    background-color: #fff;
    padding: 40px;
    border-radius: 20px;
  }
  //登录按钮
  .loginBtn {
    width: 48%;
  }
  //标题样式
  h2 {
    margin-bottom: 10px;
  }
}
</style>
